///////////////////////////////////////////////////////////////////////
// DFileSystemImage.cpp
//
// Written by Nic Wilson

#include "StdAfx.h"
#include "DFileSystemImage.h"
#include "DFileSystemImageEvent.h"
#include "globals.h"

CDFileSystemImage::CDFileSystemImage(void)
: m_FileSystemImage(NULL)
{
}

CDFileSystemImage::~CDFileSystemImage(void)
{
	if (m_FileSystemImage != NULL)
	{
		m_FileSystemImage->Release();
	}
}

///////////////////////////////////////////////////////////////////////
//
// CDFileSystemImage::Initialize()
//
// Description:
//		Creates and initializes the IDFileSystemImage interface
//
bool CDFileSystemImage::Initialize()
{
	ASSERT(m_FileSystemImage == NULL);
	
	// Initialize the IDFileSystemImage Interface
	//m_hResult = CoCreateInstance(__uuidof(CLSID_MsftFileSystemImage), NULL, CLSCTX_INPROC_SERVER, __uuidof(IFileSystemImage), (void**)&m_DFileSystemImage);
	HRESULT hr = S_OK;
	if (GetOSVersion() < WINDOWSVISTA)
		hr = CoCreateInstance(CLSID_MsftFileSystemImage, NULL, CLSCTX_ALL, __uuidof(IFileSystemImage), (void**)&m_FileSystemImage);
	else
		hr = CoCreateInstance(CLSID_MsftFileSystemImage, NULL, CLSCTX_ALL, __uuidof(IFileSystemImage3), (void**)&m_FileSystemImage);
	if (!SUCCEEDED(hr))
	{
		m_errorMessage.Format(_T("Unable to Initialize IDFileSystemImage - Error:0x%08x"), m_hResult);
		return false;
	}
	return true;
}

bool CDFileSystemImage::CreateEvent(HWND hNotificationWnd)
{
	if (hNotificationWnd == NULL)
		return false;
	m_hNotificationWnd = hNotificationWnd;

	// Create the event sink
	eventSink = CDFileSystemImageEvent::CreateEventSink();
	if (eventSink == NULL)
	{
		m_errorMessage = _T("Unable to create event sink");
		return false;
	}

	if (!eventSink->ConnectDFileSystemImage(this))
	{
		m_errorMessage = _T("Unable to connect event sink with interface");
		return false;
	}

	eventSink->SetHwnd(m_hNotificationWnd);
	return true;
}

void CDFileSystemImage::DeleteEvent()
{
	delete eventSink;
}

IFileSystemImage* CDFileSystemImage::GetFileSystemImagePtr()
{
	return m_FileSystemImage;
}
